﻿using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.Integrated;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace ProTrack.Services.Integrated
{
    public class VendorServices : IVendorServices
    {
        public VendorServices(IBaseRepository db)
        {
            Db = db;
        }

        public IBaseRepository Db { get; }

        public async Task<bool> AddAsync(Vendor model)
        {
            var result = await Db.InsertAsync(model);
            return result;
        }

        public async  Task<bool> DeleteAsync(int[] Ids)
        {
            string sql = "delete Vendor where Id in @Ids";
            var result = await Db.DeleteAsync(sql, new { Ids });
            return result;
        }

        public async  Task<bool> DeleteAsync(int Id)
        {
            string sql = "delete Vendor where Id =@Id ";
            var result = await Db.DeleteAsync(sql, new { Id });
            return result;
        }

        public async Task<IEnumerable<Vendor>> GetAllAsync()
        {
            var sql = "select * from Vendor where 1=1 ";
            return await Db.QueryAllAsync<Vendor>(sql);
        }

        public async Task<IEnumerable<Vendor>> GetAsync(int PageIndex, int PageSize, string VendorCode, string VendorName, string VendorNick, string VendorEn, string Tel, string VendorLevel, string EnableFlag)
        {
            string sql = "select * from Vendor where 1=1";
            if (!string.IsNullOrEmpty(VendorCode))
            {
                sql += " and VendorCode like @VendorCode ";
            }


            if (!string.IsNullOrEmpty(VendorName))
            {
                sql += " and VendorName like @VendorName ";
          
            }
            if (!string.IsNullOrEmpty(VendorNick))
            {
                sql += " and VendorNick like @VendorNick ";
            }
            if (!string.IsNullOrEmpty(VendorEn))
            {
                sql += " and VendorEn like @VendorEn ";
            }
            if (!string.IsNullOrEmpty(Tel))
            {
                sql += " and Tel like @Tel ";
            }
            if (!string.IsNullOrEmpty(VendorLevel))
            {
                sql += " and VendorLevel like @VendorLevel ";
            }
            if (!string.IsNullOrEmpty(EnableFlag))
            {
                sql += " and EnableFlag like @EnableFlag ";
            }



            sql += "order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only";

            var list = await Db.QueryAsync<Vendor>(sql, new { PageIndex, PageSize, VendorCode = $"%{VendorCode}%", VendorName = $"%{VendorName}%", VendorNick = $"%{VendorNick}%", VendorEn = $"%{VendorEn}%", Tel = $"%{Tel}%", VendorLevel = $"%{VendorLevel}%", EnableFlag = $"%{EnableFlag}%" });
            return list;
        }

        public async  Task<int> GetCountAsync(string VendorCode, string VendorName,string VendorNick,string VendorEn,string Tel,string VendorLevel, string EnableFlag)
        {
            string sql = "select count(*) from Vendor where 1=1 ";
            if (!string.IsNullOrEmpty(VendorCode))
            {
                sql += " and VendorCode like @VendorCode ";
            }

            if (!string.IsNullOrEmpty(VendorName))
            {
                sql += " and VendorName like @VendorName ";
            }

            if (!string.IsNullOrEmpty(VendorNick))
            {
                sql += " and VendorNick like @VendorNick ";
            }
            if (!string.IsNullOrEmpty(VendorEn))
            {
                sql += " and VendorEn like @VendorEn ";
            }
            if (!string.IsNullOrEmpty(Tel))
            {
                sql += " and Tel like @Tel ";
            }
            if (!string.IsNullOrEmpty(VendorLevel))
            {
                sql += " and VendorLevel like @VendorLevel ";
            }
            if (!string.IsNullOrEmpty(EnableFlag))
            {
                sql += " and EnableFlag like @EnableFlag ";
            }

            var count = await Db.GetRecoreCountAsync(sql, new { VendorCode = $"%{VendorCode}%", VendorName = $"%{VendorName}%", VendorNick = $"%{VendorNick}%", VendorEn = $"%{VendorEn}%", Tel = $"%{Tel}%", VendorLevel = $"%{VendorLevel}%", EnableFlag = $"%{EnableFlag}%" });
            return count;
        }

        public async  Task<bool> UpdateAsync(Vendor model)
        {
            var result = await Db.UpdateAsync(model);
            return result;
        }
    }
}
